package openbang.account.web.portal.widget;

import java.util.List;

import openbang.common.vo.SelectModel;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * 弹出选择框
 * 
 * 支持：多选、单选
 * 
 * @author aibozeng
 *
 */

public class SelectDialog  extends DialogBox{
	//根
	private VerticalPanel vPanelRoot = new VerticalPanel();
	//选择框区域
	private VerticalPanel vPanelSelect = new VerticalPanel();
	
	//按钮栏 
	private HorizontalPanel vPanelButton = new HorizontalPanel();
	private Button selectAllButton = new Button("全选/反选");
	private Button confirmButton = new Button("确定");
	private Button closeButton = new Button("关闭");
	
	//使用者注册单击“确定”而关闭窗口的事件
	private OpenBangClickHandler confirmClickHandler = null;
	
    public SelectDialog(String title , List<SelectModel> models ){    	
    	setText(title);
    	setWidget(vPanelRoot);
    	vPanelRoot.add(vPanelSelect);
    	vPanelRoot.add(vPanelButton);
    	vPanelButton.add(selectAllButton);
    	vPanelButton.add(confirmButton);
    	vPanelButton.add(closeButton);
    	setAnimationEnabled(true);
	    setGlassEnabled(true);
	    selectAllButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//逻辑：如果全选状态,全置为不选,只要有一个为未选,则置为全选
				int cNum = vPanelSelect.getWidgetCount();
				//判断是置为全选，还是全不选
				boolean nowSelect = true;
				for(int i=0;i<cNum;i++){
					CheckBox cb = (CheckBox)vPanelSelect.getWidget(i);
					if(!cb.getValue()){
						nowSelect = false;
					}
				}
				for(int i=0;i<cNum;i++){
					OpenCheckBox cb = (OpenCheckBox)vPanelSelect.getWidget(i);
					if(cb.getValue()==nowSelect){
						cb.setValue(!nowSelect);						
					}
				}				
			}
		});
	    closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();
			}
		});
	    confirmButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//通知使用者
				if(confirmClickHandler!=null){					
					confirmClickHandler.onClick();
				}
				hide();
			}
		});
	    
	    //初始化 复选框
	    for(SelectModel model : models){
	       OpenCheckBox cb = new OpenCheckBox(model);	       
	       vPanelSelect.add(cb);
	    }
	    
    }
    
    @Override
	protected void onPreviewNativeEvent(NativePreviewEvent preview) {
		    super.onPreviewNativeEvent(preview);

		    NativeEvent evt = preview.getNativeEvent();
		    if (evt.getType().equals("keydown")) {
		      // Use the popup's key preview hooks to close the dialog when either
		      // enter or escape is pressed.
		      switch (evt.getKeyCode()) {
		        case KeyCodes.KEY_ENTER:
		        case KeyCodes.KEY_ESCAPE:
		          hide();
		          break;
		      }
		    }
    }

	public OpenBangClickHandler getConfirmClickHandler() {
		return confirmClickHandler;
	}

	public void setConfirmClickHandler(OpenBangClickHandler confirmClickHandler) {
		this.confirmClickHandler = confirmClickHandler;
	}    
    
}
